package com.elock.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.elock.base.core.Filter;
import com.elock.base.core.controller.BaseController;
import com.elock.entity.CompanyInfo;
import com.elock.entity.Member;
import com.elock.entity.MemberSaleOrg;
import com.elock.entity.PcMenu;
import com.elock.service.CarService;
import com.elock.service.CompanyInfoService;
import com.elock.service.ElockService;
import com.elock.service.MemberSaleOrgBaseService;
import com.elock.service.MemberService;
import com.elock.service.PcMenuBaseService;
import com.elock.service.TerminalService;

/**
 * Controller - 共用
 * @author 仁德
 */
@Controller("indexController")
@RequestMapping("/index")
public class IndexController extends BaseController {

	@Resource(name = "memberServiceImpl")
	private MemberService memberService;
	@Resource(name = "pcMenuBaseServiceImpl")
	private PcMenuBaseService pcMenuBaseService;
	@Resource(name = "memberSaleOrgBaseServiceImpl")
	private MemberSaleOrgBaseService memberSaleOrgBaseService;
	@Resource(name = "elockServiceImpl")
	private ElockService elockService;
	@Resource(name = "carServiceImpl")
	private CarService carService;
	@Resource(name = "terminalServiceImpl")
	private TerminalService terminalService;
	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;

	/**
	 * 主页
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(ModelMap model) {

		Member member = memberService.getCurrent();
		model.addAttribute("member", member);

		List<PcMenu> menus = null;
		if (member.getCompanyInfoId() == null) {
			menus = pcMenuBaseService.getCurrentMenuList(0);
		}
		else {
			menus = pcMenuBaseService.getCurrentMenuList(1);
		}
		model.addAttribute("menu_list", menus); // 菜单列表
		model.addAttribute("menu_list", menus); // 菜单列表
		CompanyInfo companyInfo = companyInfoService.getCurrent();
		model.addAttribute("companyInfo", companyInfo);

		return "/index";
	}

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(ModelMap model) {

		Member member = memberService.getCurrent();
		model.addAttribute("member", member);

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("member", member));
		MemberSaleOrg memberSaleOrg = memberSaleOrgBaseService.find(filters);
		if (memberSaleOrg != null) {
			model.addAttribute("saleOrg", memberSaleOrg.getSaleOrg());
		}
		model.addAttribute("countElock", elockService.count());
		model.addAttribute("countCar", carService.count());
		model.addAttribute("countTerminal", terminalService.count());

//		BigDecimal quantity = new BigDecimal(0);
//		List<Cart> carts = cartService.getCurrent();
//		if (carts.size() > 0) {
//			for (Cart cart : carts) {
//				quantity = quantity.add(cart.getQuantity());
//			}
//		}
//		model.addAttribute("cartQuantity", quantity.intValue());
//
//		List<Order> orders = new ArrayList<Order>();
//		List<Filter> filters = new ArrayList<Filter>();

		/**公告*/
//		orders.add(Order.asc("order"));
//		filters.add(Filter.eq("isPublic", true));
//		List<SystemNotice> systemNotices = systemNoticeService.findList(null,
//				filters,
//				orders);
//		model.addAttribute("systemNotices", systemNotices);

		/**快捷通道*/
//		orders.clear();
//		filters.clear();
//		orders.add(Order.asc("order"));
//		filters.add(Filter.eq("isEnabled", true));
//		List<QuickLink> quickLinks = quickLinkService.findList(6,
//				filters,
//				orders);
//		model.addAttribute("quickLinks", quickLinks);

		/**产品分类*/
//		orders.clear();
//		filters.clear();
//		orders.add(Order.asc("order"));
//		filters.add(Filter.eq("isEnabled", true));
//		filters.add(Filter.isNull("parent"));
//		List<ProductCategory> productCategorys = productCategoryBaseService.findList(20,
//				filters,
//				orders);
//		model.addAttribute("productCategorys", productCategorys);

		/**待办分类*/
//		orders.clear();
//		filters.clear();
//		orders.add(Order.asc("order"));
//		filters.add(Filter.eq("isEnabled", true));
//		List<StatisticsCategory> statisticsCategorys = statisticsCategoryService.findList(2,
//				filters,
//				orders);
//
//		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		if (storeMember != null) {
//			for (StatisticsCategory statisticsCategory : statisticsCategorys) {
//				List<Statistics> statisticss = statisticsService.findListForHomePage(statisticsCategory.getId(),
//						storeMember.getId(),
//						6);
//				Map<String, Object> data = new HashMap<String, Object>();
//				data.put("statisticsCategory", statisticsCategory);
//				data.put("statisticss", statisticss);
//				list.add(data);
//			}
//		}
//		model.addAttribute("list", list);
//
//		filters.clear();
//		filters.add(Filter.eq("member", storeMember.getMember()));
//		List<StoreMember> storeMembers = storeMemberBaseService.findList(null,
//				filters,
//				null);
//		Store store = null;
//		BigDecimal storeBalance = null;
//		BigDecimal creditBalance = null;
//		for (StoreMember storeMember2 : storeMembers) {
//			if (!storeMember2.getStore().getType().equals(Type.enterprise)) {
//				store = storeMember2.getStore();
//				storeBalance = storeBalanceService.findBalance(store.getId());
//				creditBalance = storeBalanceService.findCreditBalance(store.getId());
//				break;
//			}
//		}
//		model.addAttribute("store", store);
//		model.addAttribute("storeBalance", storeBalance);
//		model.addAttribute("creditBalance", creditBalance);

		/**热销产品*/
//		List<Map<String, Object>> hotSalesProducts = hotSalesProductService.findShow(null,
//				null,
//				null,
//				null,
//				null,
//				store == null ? null : store.getId(),
//				null,
//				12);
//		model.addAttribute("hotSalesProducts", hotSalesProducts);

		return "/home";
	}

}